package com.web.user;

import com.bean.User;
import com.service.UserService;
import org.apache.commons.io.FileUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class UpdateUserServlet extends HttpServlet {
    UserService userService=new UserService();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 接收用户行为
        String actionName = request.getParameter("actionName");
        if ("userHead".equals(actionName)) {

            // 加载头像
            userHead(request, response);

        }
    }

    /**
     * 加载头像
     *  1. 获取参数 （图片名称）
     *  2. 得到图片的存放路径 （request.getServletContext().getealPathR("/")）
     *  3. 通过图片的完整路径，得到file对象
     *  4. 通过截取，得到图片的后缀
     *  5. 通过不同的图片后缀，设置不同的响应的类型
     *  6. 利用FileUtils的copyFile()方法，将图片拷贝给浏览器
     * @param request
     * @param response
     */
    private void userHead(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 获取参数 （图片名称）
        String head = request.getParameter("imageName");
        // 2. 得到图片的存放路径 （得到项目的真实路径：request.getServletContext().getealPathR("/")）
        String realPath = request.getServletContext().getRealPath("/");
        // 3. 通过图片的完整路径，得到file对象
        File file = new File(realPath + "/" + head);
        // 4. 通过截取，得到图片的后缀
        String pic = head.substring(head.lastIndexOf(".")+1);
        // 5. 通过不同的图片后缀，设置不同的响应的类型
        if ("PNG".equalsIgnoreCase(pic)) {
            response.setContentType("image/png");
        } else if ("JPG".equalsIgnoreCase(pic) || "JPEG".equalsIgnoreCase(pic)) {
            response.setContentType("image/jpeg");
        } else if ("GIF".equalsIgnoreCase(pic)) {
            response.setContentType("image/gif");
        }
        // 6. 利用FileUtils的copyFile()方法，将图片拷贝给浏览器
        FileUtils.copyFile(file, response.getOutputStream());
    }
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws  ServletException, IOException {
        //1、拿东西
        User user = (User)(req.getSession().getAttribute("user"));
        String username = user.getUsername();
        //System.out.println(username);

        String password=req.getParameter("password");
        String nick = req.getParameter("nick");
        String mood = req.getParameter("mood");
        String head = req.getParameter("head");
//        System.out.println("nick:" + nick);
        //String nick=new String(req.getParameter("nick").getBytes("ISO-8859-1"),"UTF-8");



        //2、调用service完成登录操作
        try {
            user = userService.findUserByNameAndPwd(user.getUsername(),user.getPassword());
            user.setPassword(password);//更新user里面的东西
            user.setNick(nick);
            user.setMood(mood);
            user.setHead(head);

            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("修改用户信息失败！！！");
        }
        //System.out.println(user.getUsername() + user.getMood() + user.getPassword());

        //3、修改成功，更新session
        req.getSession().setAttribute("user",user);
        resp.sendRedirect(req.getContextPath() + "success.jsp");


    }
}
